﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;

namespace Shared
{
    public static class TitleMonitor
    {
        #region Fields

        /// <summary>
        /// Calling module name.
        /// </summary>
        private static string Module = "";

        /// <summary>
        /// Monitor process thread.
        /// </summary>
        private static Thread MonitorThread = new Thread(new ThreadStart(MonitorProcess));

        /// <summary>
        /// Start time.
        /// </summary>
        private static DateTime StartTime;

        #endregion

        #region Methods

        /// <summary>
        /// Initializes the title monitor.
        /// </summary>
        /// <param name="ServerModule">Name of the calling server module.</param>
        public static void Initialize(string ServerModule)
        {
            Module = ServerModule;
        }

        /// <summary>
        /// Starts the title monitor.
        /// </summary>
        public static void Start()
        {
            StartTime = DateTime.Now;

            MonitorThread.Name = "TitleMonitor";
            MonitorThread.Priority = ThreadPriority.Lowest;
            MonitorThread.Start();
        }

        /// <summary>
        /// Stops the title monitor.
        /// </summary>
        public static void Stop()
        {
            MonitorThread.Abort();
        }

        /// <summary>
        /// Process for the title monitor thread.
        /// </summary>
        private static void MonitorProcess()
        {
            try
            {
                while (true)
                {
                    Console.Title = Module + " [ Memory: " + GC.GetTotalMemory(false) / 1024 + " KB ] [ Uptime: " + (DateTime.Now - StartTime).ToString().Substring(0, 8) + " ]";

                    Thread.Sleep(1000);
                }
            }
            catch { }
        }

        #endregion
    }
}
